package com.china08.yxyapi.api.home.kecheng.sync;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lessons.YxyLessonsUserScore;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyLessonsUserScoreRepository;

/**
 * 同步课程--观看视频页面--课程评价(没评价返回false，已评价回显)
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncLessonUserScoreController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsUserScoreRepository yxyLessonsUserScoreRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/user/score/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String lessonsId = reqModel.lessonsId;

        YxyLessonsUserScore yxyLessonsUserScores = yxyLessonsUserScoreRepository.findByUserIdAndLessonsId(userId,
                lessonsId);
        RespModel model = new RespModel();
        // 已评价
        if (null != yxyLessonsUserScores) {
            model.score = yxyLessonsUserScores.getScore();
            model.status = true;
        }
        // 未评价
        else {
            model.status = false;
        }

        return model;

    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonsId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // true已评价、false未评价
        public Boolean status;

        public Integer score;
    }
}
